package com.example.goods_service.controller;

import com.example.goods_service.dao.Goods_gfc;
import com.example.goods_service.return_data.Mall_Data;
import com.example.goods_service.service.rpc.User_servcie;
import com.example.goods_service.vo.SearchRecords;
import com.example.goods_service.vo.TCommodity;
import com.example.goods_service.vo.T_commodity;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class Gfc_contorller {
    @Autowired
    GoodsController_liujingxi goodsController_liujingxi;
    @Autowired
    GoodsController_oyhl goodsController_oyhl;
    @Autowired
    Mall_Data mall_data;
    @Autowired
    User_servcie user_servcie;
    @Autowired
    Goods_gfc goods_gfc;

    @ApiOperation("猜你喜欢")
    @RequestMapping("/g_cnxh")
    public Mall_Data g_cnxh(@RequestParam(required = false) Integer userid) {
        System.out.println(userid);
        if (userid==null){
            return new Mall_Data().setdata(goods_gfc.ppp1(0)).setcode(1);
        }
        List<Integer> goodsid_s = new ArrayList<>();
        Map<Integer, Integer> goodstype_s = new HashMap<Integer, Integer>();
        //展示20个记录
        final int max = 20;
        //最近记录查询到userid
        Mall_Data mall_data = user_servcie.g_welcome(userid);
        System.out.println(mall_data);
        List<Integer> goodss = (List<Integer>) mall_data.get("data");
        //没有浏览记录
        if (goodss==null){
            return new Mall_Data().setdata(goods_gfc.ppp1(0)).setcode(1);
        }
       //有浏览记录 查看类型比例
        for (Integer z : goodss) {
           if (goodstype_s.get(z)==null){
               goodstype_s.put(z,1);
           }else {
               goodstype_s.put(z,goodstype_s.get(z)+1);
           }

        }

        System.out.println(goodstype_s);
        //出现数最大数
        int maxz = 0;
        //查询因子
        double bd = 0;
        //查询出的猜你喜欢集合
        Set<TCommodity> list = new HashSet<TCommodity>();
        Set<Map.Entry<Integer, Integer>> entries = goodstype_s.entrySet();
        for (Map.Entry<Integer, Integer> entry : entries) {
            maxz += entry.getValue();
        }
        bd = maxz / max;
        for (Map.Entry<Integer, Integer> entry : entries) {
            Integer key = entry.getKey();
            int z = entry.getValue();
            list.addAll(goods_gfc.ppp(key, (int) (z+(bd*20))));
        }
        //没有这么多商品则随机给
        if (list.size()<20){
            list.addAll(goods_gfc.ppp1(0));
        }
        return new Mall_Data().setdata(list).setcode(1);
    }

}
